package com.CGFrame.struts.bean.employee;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;


import com.CGFrame.struts.POJOs.Employee;
import com.CGFrame.struts.POJOs.Sector;
import com.CGFrame.struts.POJOs.Status;

import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

public class EmployeeBean {
//	private Employee employee=null;
	public Vector queryDetail(Employee employee)
	{
		
		Vector v = new Vector();
		String[] str_Name = {"Name",employee.getEmpName()+" "};
		v.add(str_Name);
		if(employee.getGender().equals("M"))
		{
			String[] str_Gender = {"Gender","Male"};
			v.add(str_Gender);
		}
		else 
		{
			String[] str_Gender = {"Gender","Female"};
			v.add(str_Gender);
		}
		String[] str_Birthday = {"Birthday",""+employee.getBirthday().toString().substring(0,10)};
		v.add(str_Birthday);
		String[] str_Address = {"Address",employee.getAddress()+" "};
		v.add(str_Address);
		String[] str_TelNo = {"TelNo.",employee.getTelNo()+" "};
		v.add(str_TelNo);
		String[] str_MobileNo = {"MobileNo.",employee.getMobileNo()+" "};
		v.add(str_MobileNo);
		String[] str_FamilyNo = {"FamilyNo.",employee.getFamilyNo()+" "};
		v.add(str_FamilyNo);
		String[] str_ID = {"ID card",employee.getIdcard()+" "};
		v.add(str_ID);
		String[] str_Email = {"e-mail",employee.getEmail()+" "};
		v.add(str_Email);
		
		return v;
	}
	public Employee getEmployee(String employeeId)
	{
		
			ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
			return (Employee)pop.UniqueQuery("Employee","employeeId,"+employeeId);
		
		
	}
	public static Employee update(String employeeId, String address, String birthday, String email, String familyNo, String gender, String idcard, String mobileNo, String name, String sectorId, String telNo) {
		// TODO Auto-generated method stub
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		
				
		Employee employee = (Employee)pop.UniqueQuery("Employee","employeeId,"+employeeId);
		Date dat_Birthday = new Date();
		try
		{
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			dat_Birthday  = df.parse(birthday);
		}
		catch(Exception e)
		{
		}
		Sector sector = (Sector)pop.UniqueQuery("Sector","sectorId,"+sectorId);
		employee.setEmpName(name);
		employee.setGender(gender);
		employee.setIdcard(idcard);
		employee.setSector(sector);
		employee.setBirthday(dat_Birthday);
		employee.setTelNo(telNo);
		employee.setFamilyNo(familyNo);
		employee.setMobileNo(mobileNo);
		employee.setEmail(email);
		employee.setAddress(address);
		Status status = (Status)pop.UniqueQuery("Status", "statusName,HasInput,statusType,Employee");
		employee.setStatus(status);
		pop.saveObject(employee);
		return employee;
	}
	public void deleteEmployee(String employeeId){
		// TODO Auto-generated method stub
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		int emp_Id = Integer.parseInt(employeeId);
		pop.deleteObject("Employee", emp_Id);
	}
	
	public static ArrayList getEmployees(){
		ArrayList list=new ArrayList();
		
		//list的第一个元素,包含表头信息
		LinkedList<String> ll=new LinkedList<String>();
		ll.offer("name");
		ll.offer("gender");
		ll.offer("telNO");
		ll.offer("familyNO");
		ll.offer("mobileNO");
		ll.offer("address");
		ll.offer("IDCard");
		ll.offer("birthday");
		ll.offer("email");
		list.add(ll);
		
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		List employees=pop.Query("Employee", null, null, null);
		for(Iterator iter=employees.iterator();iter.hasNext();){
			Employee ep=(Employee)iter.next();
			LinkedList emp=new LinkedList();
			emp.offer(ep.getEmpName());
			emp.offer(ep.getGender());
			emp.offer(ep.getTelNo());
			emp.offer(ep.getFamilyNo());
			emp.offer(ep.getMobileNo());
			emp.offer(ep.getAddress());
			emp.offer(ep.getIdcard());
			emp.offer(ep.getBirthday());
			emp.offer(ep.getEmail());
			list.add(emp);
		}
		return list;
	}
}
